package com.gitee.kenewstar.migration.api;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.gitee.kenewstar.migration.entity.CommonResult;
import com.gitee.kenewstar.migration.entity.TemplateHeader;
import com.gitee.kenewstar.migration.service.migration.ImportTmplMigrationService;
import com.gitee.kenewstar.migration.util.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author kenewstar
 */
@DS("dev")
@RestController
@RequestMapping("/import")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ImportApi {

    private final ImportTmplMigrationService importTmplMigrationService;


    @PostMapping("/import-migration")
    public List<CommonResult> importMigration(@RequestBody List<String> tmplCodeList, @RequestParam(required = false) Long tenantId) {
        return this.importTmplMigrationService.importTmplMigration(tmplCodeList, tenantId);
    }

    @PostMapping("/excel-import-migration")
    public List<CommonResult> excelImportMigration(@RequestPart("file") MultipartFile file) {
        return this.importTmplMigrationService.excelImportMigration(ExcelUtil.filterReadData(file, TemplateHeader.class));
    }


}
